package com.sfh.cp.user.pst;

import android.text.TextUtils;

import com.sfh.cp.user.login.UserLoginActivity;
import com.sfh.lib.AppCacheManager;
import com.sfh.lib.mvp.service.AbstractPresenter;

import java.util.Date;
import java.util.List;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.datatype.BmobDate;
import cn.bmob.v3.listener.FindListener;
import cn.bmob.v3.listener.GetListener;
import cn.bmob.v3.listener.UpdateListener;
import crm.sfh.server.Const;
import crm.sfh.server.MangerUser;
import crm.sfh.server.SSigin;
import crm.sfh.server.SUser;

/**
 * 功能描述:
 *
 * @date 2018/5/24
 */
public class PstUser extends AbstractPresenter<IPstUser.IVUser> implements IPstUser {
    @Override
    public void getUser() {
        SUser user = AppCacheManager.newInstance().getCache(Const.USER, new SUser());
        if (TextUtils.isEmpty(user.getObjectId())) {
            return;
        }
        getView().onUser(user.getUser());
    }

    @Override
    public void sign() {
       final SUser user = AppCacheManager.newInstance().getCache(Const.USER, new SUser());
        if (TextUtils.isEmpty(user.getObjectId())) {
            return;
        }
        BmobQuery<SSigin> siginQuery = new BmobQuery<>();
        siginQuery.addWhereEqualTo("userId",user.getObjectId());
        Date start = new Date();
        start.setHours(0);
        start.setMinutes(0);
        start.setSeconds(0);
        siginQuery.addWhereGreaterThanOrEqualTo("createdAt",new BmobDate(start));
        Date end = new Date();
        end.setHours(23);
        end.setMinutes(59);
        end.setSeconds(59);
        siginQuery.addWhereLessThanOrEqualTo("createdAt",new BmobDate(end));

        siginQuery.findObjects(AppCacheManager.getApplication(), new FindListener<SSigin>() {
            @Override
            public void onSuccess(List<SSigin> list) {
                if (list == null || list.isEmpty() ){
                    new MangerUser().updateWealth(AppCacheManager.getApplication(), user.getObjectId(), 1, new UpdateListener() {
                        @Override
                        public void onSuccess() {
                            SSigin sSigin = new SSigin();
                            sSigin.setUserId( user.getObjectId());
                            sSigin.save(AppCacheManager.getApplication());

                            BmobQuery<SUser> query = new BmobQuery<>();
                            query.getObject(AppCacheManager.getApplication(), user.getObjectId(), new GetListener<SUser>() {
                                @Override
                                public void onSuccess(SUser sUser) {
                                    AppCacheManager.newInstance().putCache(true,Const.USER, sUser);
                                    getView().onUser(sUser.getUser());
                                }

                                @Override
                                public void onFailure(int i, String s) {

                                }
                            });
                        }

                        @Override
                        public void onFailure(int i, String s) {
                            getView().showToast("签到失败");
                        }
                    });
                }else{
                    getView().showToast("已签到");
                }
            }

            @Override
            public void onError(int i, String s) {

            }
        });

    }
}
